package com.haiertvbic.tvnow.pop;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.adapter.ActorsAdapter;
import com.haiertvbic.tvnow.model.Actor;
import com.haiertvbic.tvnow.model.ActorType;
import com.haiertvbic.tvnow.model.ProgramDetail;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.network.ImageUtil;
import com.haiertvbic.tvnow.util.SLog;

public class ProgramDetailPop extends PopWindow {
	private static final String TAG = "ProgramDetailPop";
	private Activity context;
	private ProgramDetail program;
	private View childView;
	private PopupWindow pop;

	public ProgramDetailPop(Activity context, View childView,
			ProgramDetail program) {
		this.context = context;
		this.childView = childView;
		this.program = program;
	}

	@Override
	public void showPop() {
		dismissPop();
		new PosterAsyncTask().execute(program);
	}

	@Override
	public void dismissPop() {
		if (pop != null && pop.isShowing()) {
			pop.dismiss();
			pop = null;
		}
	}

	class PosterAsyncTask extends AsyncTask<Object, Void, Void> {

		Bitmap posterBit = null;
		ArrayList<Actor> actors;
		ArrayList<ActorType> actorTypeList = new ArrayList<ActorType>();
		ViewHodler hodler = new ViewHodler();

		class ViewHodler {
			ProgressBar pb;
			ImageView poster;
			TextView actor;
			GridView actorGV;
			TextView director;
			GridView directorGV;
			TextView other;
			GridView otherGV;
		}

		@Override
		protected void onPreExecute() {
			View connvert = LayoutInflater.from(context).inflate(
					R.layout.pop_program_detail, null);
			int[] arrayOfInt = new int[2];
			if (childView != null) {
				childView.getLocationInWindow(arrayOfInt);
				float pictureHeight = context.getResources().getDimension(
						R.dimen.picture_height);
				float pictureWidth = context.getResources().getDimension(
						R.dimen.picture_width);
				float screen = context.getWindowManager().getDefaultDisplay()
						.getWidth();
				pictureWidth = 450f;
				pictureHeight = 230f;
				// 当条目被选中弹出popwindow
				pop = new PopupWindow(connvert, (int) pictureWidth,
						(int) (pictureHeight + 0.5f));
				pop.setBackgroundDrawable(new BitmapDrawable());
				pop.setFocusable(true);
				pop.showAtLocation(childView, Gravity.LEFT | Gravity.TOP,
						(int) (0 - pictureWidth), arrayOfInt[1]);
				hodler.pb = (ProgressBar) connvert
						.findViewById(R.id.progressBar_js);
				hodler.poster = (ImageView) connvert
						.findViewById(R.id.detail_iv);
				hodler.director = (TextView) connvert
						.findViewById(R.id.director);
				hodler.actor = (TextView) connvert.findViewById(R.id.actor);
				hodler.directorGV = (GridView) connvert
						.findViewById(R.id.director_gv);
				hodler.actorGV = (GridView) connvert
						.findViewById(R.id.actor_gv);

				hodler.other = (TextView) connvert.findViewById(R.id.other);
				hodler.otherGV = (GridView) connvert
						.findViewById(R.id.other_gv);
				ScaleAnimation sa = new ScaleAnimation(0.4f, 1.0f, 0.4f, 1.0f,
						screen, 0f);
				sa.setDuration(800);
				connvert.startAnimation(sa);
				hodler.pb.setVisibility(View.VISIBLE);
			}
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Object... params) {
			ProgramDetail program = (ProgramDetail) params[0];
			String posterUrl;
			posterUrl = program.getPicurl();
			posterBit = ImageUtil.getPicData(posterUrl);
			actors = EPGWeb.getActorsInfo(context, program.getPid());
			SLog.d(TAG, "pid===" + program.getPid());
			if (actors != null) {
				SLog.d(TAG, "actors===" + actors.size());
			} else {
				SLog.d(TAG, "actors===null");
			}
			for (int i = 0; actors != null && i < actors.size(); i++) {
				Actor actor = actors.get(i);
				String starT = actor.getStarType();
				if (starT.trim().length() < 1) {
					// starT = "Other";
					starT = "其他";
				}
				if (listContains(starT)) {
					getType(starT).add(actor);
				} else {
					ArrayList<Actor> actorTemp = new ArrayList<Actor>();
					actorTemp.add(actor);

					ActorType actorType = new ActorType();
					actorType.setStarType(starT);
					actorType.setActros(actorTemp);
					actorTypeList.add(actorType);
				}
			}
			return null;
		}

		private ArrayList<Actor> getType(String starT) {
			ArrayList<Actor> temp = null;
			for (int i = 0; i < actorTypeList.size(); i++) {
				if (actorTypeList.get(i).getStarType().equals(starT)) {
					temp = actorTypeList.get(i).getActros();
				}
			}
			return temp;
		}

		private boolean listContains(String starT) {
			boolean isContain = false;
			for (int i = 0; i < actorTypeList.size(); i++) {
				if (actorTypeList.get(i).getStarType().equals(starT)) {
					isContain = true;
					break;
				}
			}
			return isContain;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			hodler.pb.setVisibility(View.GONE);
			if (posterBit == null && actors == null) {
				Toast.makeText(context,
						context.getString(R.string.no_relatve_detail), 1)
						.show();
				dismissPop();
			} else {
				if (posterBit != null) {
					hodler.poster.setVisibility(View.VISIBLE);
					hodler.poster.setImageBitmap(posterBit);
				}
				if (actors != null && actors.size() > 0) {
					if (actorTypeList.size() >= 1) {
						hodler.director.setVisibility(View.VISIBLE);
						hodler.director.setText(actorTypeList.get(0)
								.getStarType() + "：");
						// hodler.directorGV.setFocusable(true);
						// hodler.directorGV.requestFocus();
						ArrayList<Actor> actors = actorTypeList.get(0)
								.getActros();
						hodler.directorGV.setAdapter(new ActorsAdapter(actors,
								context));
						hodler.directorGV
								.setOnItemClickListener(new ActorItemClickListener(
										actors));
					}
					// =======================
					if (actorTypeList.size() > 1) {
						hodler.actor.setVisibility(View.VISIBLE);
						ArrayList<Actor> actors = actorTypeList.get(1)
								.getActros();
						// 大于15个演员就移除后面多余的几张
						if (actors.size() > 15) {
							int count = actors.size();
							int num = count - 15;
							for (; num != 0; count--, num--) {
								actors.remove(count - 1);
							}
						}
						// hodler.actorGV.setFocusable(true);
						// hodler.actorGV.requestFocus();
						hodler.actorGV
								.setOnItemClickListener(new ActorItemClickListener(
										actors));
						hodler.actorGV.setAdapter(new ActorsAdapter(actors,
								context));
						hodler.actor.setText(actorTypeList.get(1).getStarType()
								+ "：");
					}
					if (actorTypeList.size() > 2) {
						hodler.other.setVisibility(View.VISIBLE);
						hodler.other.setText(actorTypeList.get(2).getStarType()
								+ "：");
						ArrayList<Actor> actors = actorTypeList.get(2)
								.getActros();
						if (actorTypeList.size() > 3) {
							for (int i = 3; i < actorTypeList.size(); i++) {
								ArrayList<Actor> temp = actorTypeList.get(i)
										.getActros();
								actors.addAll(temp);
							}
						}
						// hodler.otherGV.setFocusable(true);
						// hodler.otherGV.requestFocus();
						hodler.otherGV
								.setOnItemClickListener(new ActorItemClickListener(
										actors));
						hodler.otherGV.setAdapter(new ActorsAdapter(actors,
								context));
					}
				} else {
					Toast.makeText(context,
							context.getString(R.string.no_relatve_detail), 1)
							.show();
					dismissPop();
				}
			}

		}
	}

	public class PopOnKeyListener implements OnKeyListener {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				pop.setFocusable(false);
				dismissPop();
				break;

			default:
				break;
			}
			return false;
		}

	}

	/**
	 * 弹出演员详情
	 * 
	 * @author sun
	 * 
	 */
	public class ActorItemClickListener implements OnItemClickListener {
		private ArrayList<Actor> actors;

		public ActorItemClickListener(ArrayList<Actor> actors) {
			this.actors = actors;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Actor actor = actors.get(arg2);
			// AlphaAnimation animOut = new AlphaAnimation(1, 0);
			// animOut.setDuration(500);
			// pop.s
			new ActorDetailPop(context, actor, childView, pop, program)
					.showPop();
		}

	}

	@Override
	public boolean getPopState() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setPopState(boolean b) {
		// TODO Auto-generated method stub

	}
}
